#!/bin/sh
# PCP QA Test No. 1886
# Test disjuncted qualifiers in pmseries queries
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# This test is not run if we dont have pmseries and redis installed.
_check_series

_cleanup()
{
    [ -n "$redisport" ] && redis-cli -p $redisport shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
	-e "s,$tmp.farm/farm,PATH,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here

mkdir -p $tmp.farm
tar -C $tmp.farm -xf archives/farm.tar.xz

redisport=`_find_free_port`
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test Redis server ..."
redis-server --port $redisport > $tmp.redis 2>&1 &
echo "PING"
pmsleep 0.125
redis-cli -p $redisport ping
echo

args="-p $redisport -Z UTC"

echo "== Load metric data into this redis instance"
for node in node80 node81; do
    pmseries $args --load "{source.path: \"$tmp.farm/farm/$node/20201124\"}" | _filter_source
done

pmsleep 0.2
# redis-cli -p $redisport --scan | LC_COLLATE=POSIX sort

echo
echo "== extract descriptor for kernel.all.pswitch for each host"
pmseries $args -a 1ef81375c0b3315e2b643be597b996bd04dcf49e
pmseries $args -a f91cd337e38ea1e444be3e81e4969aad689deafe

echo;echo "== Verify singular metric, no qualifier"
pmseries $args 'kernel.all.pswitch[count:2]'
echo

echo;echo "== Verify singular metric, hostname:node80 qualifier"
pmseries $args 'kernel.all.pswitch{hostname:"node80"}[count:2]'
echo

echo;echo "== Verify singular metric, hostname:node80||hostname:node81"
pmseries $args 'kernel.all.pswitch{hostname:"node80" || hostname:"node81"}[count:2]'
echo

echo;echo "== Verify singular metric, regex qualifier"
pmseries $args 'kernel.all.pswitch{hostname~~"node*"}[count:2]'
echo

# success, all done
status=0
exit
